﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GomMinhLongDO;
using GomMinhLongBL;

namespace GomMinhLong.AdminCP.PageUC
{
    public partial class ucAlbum : System.Web.UI.UserControl
    {
        private static int type = 0;
        private static cmsAlbumDO obj = new cmsAlbumDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["SP"] != null)
            {
                SP.Visible = true;
                drp.DataSource = new cmsSanPhamBL().SelectAll();
                drp.DataTextField = "TenSanPham";
                drp.DataValueField = "SanPhamID";
            }
            else
            {
                Cate.Visible = true;
                int id = 0;
                if (Request.QueryString["Type"] != null)
                    type = int.Parse(Request.QueryString["Type"].ToString());
                if (Request.QueryString["AlbumID"] != null)
                {
                    id = int.Parse(Request.QueryString["AlbumID"].ToString());
                    obj.AlbumID = id;
                    obj = new cmsAlbumBL().Select(obj);
                    txtDescription.Text = obj.Description;
                    txteDescription.Text = obj.eDescription;
                    txtTitle.Text = obj.Title;
                    txteTitle.Text = obj.eTitle;
                    cboCategory.Value = obj.CategoryID.ToString();
                    chkVideo.Checked = obj.Type;
                    hplImage.ImageUrl = "~/Media/" + obj.CoverImg;
                }
            }
        }
        private string UploadFile(FileUpload fulImage)
        {
            if (fuImage.HasFile)
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTitle.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }
        private cmsAlbumDO init()
        {
            obj.Title = txtTitle.Text;
            obj.eTitle = txteTitle.Text;
            obj.Description = txtDescription.Text;
            obj.eDescription = txteDescription.Text;
            obj.CoverImg = UploadFile(fuImage);
            obj.Type = chkVideo.Checked;
            if (Request.QueryString["SP"] != null)
                obj.SanPhamID = int.Parse(drp.SelectedValue);
            else
                obj.CategoryID = int.Parse(cboCategory.Value.ToString());
            return obj;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            obj = init();
            if (obj.AlbumID > 0)
                new cmsAlbumBL().Update(obj);
            else
                new cmsAlbumBL().Insert(obj);
            Functions.Alert("Cập nhật thành công!!", "Default.aspx?Page=ListAlbum&Type=" + type);
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?Page=ListAlbum&Type="+type);
        }
    }
}